﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Drawing.Imaging;

namespace Dogs
{
    public partial class Form1 : Form
    {
        DogsManager manager;
        SortedDictionary<long, string> AnimalsMale;
        SortedDictionary<long, string> AnimalsFeMale;
        

        public Form1()
        {
            InitializeComponent();            
            manager = new DogsManager(this);
            //Animals = manager.GetAnimalsDict();
            printdoc1.PrintPage += new PrintPageEventHandler(printdoc1_PrintPage); 
            
        }

        private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
        {

        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            String Name = textBox1.Text;
            DateTime birthdate;
            bool gender = true;
            int father_id = -1;
            int mother_id = -1;


            if (Name.Trim().Equals(""))
            {
                MessageBox.Show("Nome non valido!");
                return;
            }
                                                       
            if (radioButton1.Checked)
            {
                gender = false;
            }
            else
                if (radioButton2.Checked)
                {
                    gender = true;
                }
            else             
             MessageBox.Show("Select Gender.");

            if (comboBox1.SelectedItem != null)
            {
                father_id = comboBox1.SelectedIndex;
            }
            if (comboBox2.SelectedItem != null)
            {
                mother_id = comboBox2.SelectedIndex;
            }

            birthdate = dateTimePicker1.Value;

            if (DateUtil.IsFirstDateLater(birthdate, DateTime.Now))
            {
                MessageBox.Show("Data nascita non valida!");
                return;
            }

            if ((!DateUtil.IsDateEqual(birthdate, DateTime.Now)) && (!manager.CheckNewAnimalDateBirth(birthdate)))
            {
                MessageBox.Show("Data nascita non valida.");
                return;
            }
            
            textBox1.Text = "";
            manager.AddAnimal(Name, dateTimePicker1.Value, gender, father_id, mother_id);

        }

        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {

        }
        
        private void FiilParentsList(SortedDictionary<long, string> animals, ComboBox combobox, Dictionary<int, long> mapdict)
        {
            //combobox.Items.Clear();
            //mapdict.Clear();
            //int key = -1;

            //if ( Animals != null )
            //{
            //    foreach (KeyValuePair<long, string> item in animals)
            //    {
            //        key = combobox.Items.Add(item.Value);
            //        mapdict.Add(key, item.Key);
            //    }
            //}
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //AnimalsMale = manager.GetAnimalsDict();
            manager.FillParentsList();
            dateTimePicker1.Value = DateTime.Now;

            //if ((Animals != null) && (Animals.Count != 0))
            //{
            //    FiilParentsList(Animals, comboBox1, IndexMappingCB1);
            //    FiilParentsList(Animals, comboBox2, IndexMappingCB2);
            //}

        }

        private void BtnShowInfo_Click(object sender, EventArgs e)
        {
            if (dateTimePicker2.Enabled)
            {
                DateTime Date = dateTimePicker2.Value;
                if (DateUtil.IsFirstDateLater(Date, DateTime.Now))
                {
                    MessageBox.Show("Data nascita non valida.");
                    return;
                }
                manager.FillAnimalsbyDate(Date);
            }
            else
            {
                manager.RefreshAnimalsList();
                manager.FillAnimalsList();
            }
                                       
        }

        private void listAnimals_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        // Declare following Object Variables
        PrintDocument printdoc1 = new PrintDocument();
        PrintPreviewDialog previewdlg = new PrintPreviewDialog();
        Panel pannel1 = null;
        Panel pannel2 = null;

        // declare event handler for printing in constructor

        Bitmap MemoryImage1;
        Bitmap MemoryImage2;
        Bitmap MemoryImage3;
        public void GetPrintArea(Panel pnl1, Panel pnl2, Panel pnl3)
        {
            MemoryImage1 = new Bitmap(pnl1.Width, pnl1.Height);
            MemoryImage2 = new Bitmap(pnl2.Width, pnl2.Height);
            MemoryImage3 = new Bitmap(pnl3.Width, pnl3.Height); 
            //Rectangle rect = new Rectangle(0,0,pnl.Width ,pnl.Height);
            pnl1.DrawToBitmap(MemoryImage1, new Rectangle(0, 0, pnl1.Width, pnl1.Height));
            pnl2.DrawToBitmap(MemoryImage2, new Rectangle(0, 0, pnl2.Width, pnl2.Height));
            pnl3.DrawToBitmap(MemoryImage3, new Rectangle(0, 0, pnl3.Width, pnl3.Height));
        }

        /*protected override void OnPaint(PaintEventArgs e)
        {
            GetPrintArea(panel1); 
            e.Graphics.DrawImage(MemoryImage, 0, 0);
            base.OnPaint(e);
        }*/

        void printdoc1_PrintPage(object sender, PrintPageEventArgs e)
        {
            Rectangle pagearea = e.PageBounds;
            //pannel.Location.Y
            e.Graphics.DrawImage(MemoryImage2, (pagearea.Width / 2) - (pannel2.Width / 2), 1);
            e.Graphics.DrawImage(MemoryImage1, (pagearea.Width / 2) - (pannel1.Width / 2), pannel2.Height + 10);
            e.Graphics.DrawImage(MemoryImage3, (pagearea.Width / 2) - (pannel1.Width / 2), pannel2.Height + pannel1.Height + 20);
        }

        public void Print(Panel pnl1, Panel pnl2, Panel pnl3)
        {
            pannel1 = pnl1;
            pannel2 = pnl2;
            GetPrintArea(pnl1, pnl2, pnl3); 
            previewdlg.Document = printdoc1;
            previewdlg.ShowDialog(); 
        }

        private void PrintButton_Click(object sender, EventArgs e)
        {
            Print(panel1, panel2, panel3);
        }

        private void listAnimals_DoubleClick(object sender, EventArgs e)
        {
            int AnimalIndex = -1;
            long ID = -1;

            //AnimalIndex = listAnimals.SelectedIndex;
            if (listAnimals.SelectedValue != null)
            {
                ID = (long)listAnimals.SelectedValue;
                manager.BuildTree(ID);
            }

        }

        private void textBox1_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (!checkBox1.Checked)
            {
                dateTimePicker2.Enabled = false;
            }
            else
                dateTimePicker2.Enabled = true;
        }

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            TreeView view = sender as TreeView;
            TreeNode node;
            
            if (e.Button == MouseButtons.Right)
            {
                 // Select the clicked node
                treeView1.SelectedNode = treeView1.GetNodeAt(e.X, e.Y);
                //node = treeView1.SelectedNode;
                
                if(treeView1.SelectedNode != null)
                {
                    contextMenuStrip1.Show(treeView1, e.Location);                    
                }

            }
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            manager.ShowAnimalInfo(treeView1.SelectedNode);
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            manager.FillUpdateAnimalForm(treeView1.SelectedNode);

        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            manager.RefreshTreeView(treeView1.SelectedNode);
        }

        private void listAnimals_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                // Select the clicked node
                //treeView1.SelectedNode = treeView1.GetNodeAt(e.X, e.Y);
                
                //node = treeView1.SelectedNode;

                //if (treeView1.SelectedNode != null)
                //{
                //    contextMenuStrip1.Show(treeView1, e.Location);
                //}

            }

        }

        
    
    }
}
